package com.jxtti.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jxtti.entity.SubjectMsg;
import com.jxtti.entity.SubjectTopic;
import com.jxtti.service.SubjectMsgService;
import com.jxtti.service.SubjectTopicService;
import com.jxtti.utils.DateUtil;
import com.jxtti.utils.ExceptionUtil;
import com.jxtti.utils.ImgUtil;
import com.jxtti.utils.StringUtil;
/**
 * 
 * <br>
 * <b>功能：</b>SubjectMsgController<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */ 
@Controller
@RequestMapping("/subjectMsg") 
public class SubjectMsgController {
	
	private final static Logger log= Logger.getLogger(SubjectMsgController.class);
	
	// Servrice start
	@Autowired
	private SubjectMsgService  subjectMsgService; 
	
	@Autowired
	private SubjectTopicService  subjectTopicService; 
	
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:���������ֵ��false:����Ϊ��ֵ 
    }
	
	/**
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list") 
	public ModelAndView  list(@RequestParam Map<String, Object> data,  SubjectMsg subjectMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {
			data =  subjectMsgService.list(data, subjectMsg);
			//初始化专题
			SubjectTopic st=  subjectTopicService.queryAll(new HashMap<String, Object>()).get(0);
			data.put("subjectTopic", st);
			
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/subject/subject_msg_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return mav;
	}
	
	
	/**
	 * 加载文章信息
	 * @param data
	 * @param subjectMsg
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listData") 
	@ResponseBody
	public List<SubjectMsg>  listData(@RequestParam Map<String, Object> data,  SubjectMsg subjectMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		return subjectMsgService.queryAll(data);
	}
	
	
	
	
	@RequestMapping(value="/listDataWithoutContent") 
	@ResponseBody
	public List<SubjectMsg>  listDataWithoutContent(@RequestParam Map<String, Object> data,  SubjectMsg subjectMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		
		List<SubjectMsg> lst = this.subjectMsgService.listDataWithoutContent(data);
		return lst;
	}
	
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/subject/subject_article_add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SubjectMsg  subjectMsg, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
		
			
			if(subjectMsg.getIstop() != null &&  subjectMsg.getIstop()==1)
			{
				if(subjectMsg.getContent()!=null && !subjectMsg.getContent().equals("")){
					ImgUtil img=new ImgUtil();
					List<String> imagesPath=img.getImageSrc(subjectMsg.getContent());
					subjectMsg.setImg(StringUtils.join(imagesPath.toArray(),","));
				}	
			}
			subjectMsg.setState(1); //默认已审核.
			if(subjectMsg.getContent()!=null && !subjectMsg.getContent().equals("")){
				
				subjectMsg.setContent(StringUtil.replaceEnter(subjectMsg.getContent())); // 去除换行符
			}
			
			//如果时间不写默认为当天 。
			if(data.get("createTime")== null || data.get("createTime").toString().equals(""))
			{
				subjectMsg.setCreateTime(DateUtil.getCurrentDate());
			}
			
			int exeCount = subjectMsgService.add(subjectMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SubjectMsg  subjectMsg,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			subjectMsg = subjectMsgService.querySubjectMsgById(subjectMsg);
		
			data.put("subjectMsg", subjectMsg);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/subject/subject_article_edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	
	
	
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SubjectMsg  subjectMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 修改
			SubjectMsg subjectMsgTem=subjectMsgService.querySubjectMsgById(subjectMsg);
			
			subjectMsg.setUpdateTime(new Date());
			subjectMsg.setMenu_id(subjectMsgTem.getMenu_id());
			if(subjectMsg.getIstop() != null && subjectMsg.getIstop()==1)
			{
				if(subjectMsg.getContent()!=null && !subjectMsg.getContent().equals("")){
					ImgUtil img=new ImgUtil();
					List<String> imagesPath=img.getImageSrc(subjectMsg.getContent());
					subjectMsg.setImg(StringUtils.join(imagesPath.toArray(),","));
				}	
			}
			
			
			
			if(subjectMsg.getContent()!=null && !subjectMsg.getContent().equals("")){
				
				subjectMsg.setContent(StringUtil.replaceEnter(subjectMsg.getContent())); // 去除换行符
			}
			
			int exeCount = subjectMsgService.update( subjectMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	
	@RequestMapping(value = "/audit")
	@ResponseBody
	public Map<String, Object> audit(@RequestParam Map<String, Object> data,
			SubjectMsg  subjectMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			SubjectMsg subjectMsgTem=subjectMsgService.querySubjectMsgById(subjectMsg);
			subjectMsgTem.setState(1);
			int exeCount = subjectMsgService.update( subjectMsgTem);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		}catch(Exception e){
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	@RequestMapping(value = "/unAudit")
	@ResponseBody
	public Map<String, Object> unAudit(@RequestParam Map<String, Object> data,
			SubjectMsg  subjectMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			SubjectMsg subjectMsgTem=subjectMsgService.querySubjectMsgById(subjectMsg);
			subjectMsgTem.setState(0);
			int exeCount = subjectMsgService.update( subjectMsgTem);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		}catch(Exception e){
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	

/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			SubjectMsg  subjectMsg, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			 subjectMsg=subjectMsgService.querySubjectMsgById( subjectMsg);
			int exeCount = subjectMsgService.delete(subjectMsg);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
